using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// Author:Weiyudong
    /// </summary>
    /// 
    public partial class IsolateReasonEntry : BasePage_SOM
    {
        
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140814";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
            }
        }
        /// <summary>
        /// init the page;
        /// </summary>
        private void SetPageLoad()
        {
            if (this.Request["isolationReasonID"] != null)//the record is existed
            {
                DataTable dt = new DataTable();
                dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.GetIsolateReasonDetailByID(int.Parse(this.Request["isolationReasonID"].ToString()));
                if (dt.Rows.Count == 0)
                {
                    return;
                }
                this.hidID.Value = dt.Rows[0]["isolateReasonID"].ToString();
                this.txtIsolationReasonCode1.Text = dt.Rows[0]["Code1"].ToString();
                this.txtIsolationReasonCode2.Text = dt.Rows[0]["Code2"].ToString();
                this.txtIsolationReasonCode3.Text = dt.Rows[0]["Code3"].ToString();

                this.chkVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());

                if (this.chkVoid.Checked)
                {
                    AfterVoid();
                }
                dt.Dispose();
                dt = null;

            }
            else//new record page
            {
                this.chkVoid.Enabled = false;
            }

            //bind confirm function on btnVoid
            //string bindScript = "if (confirm('" + this.GetMessage("SOM_VoidSure") + "') )";

            //bindScript = bindScript + "{return true;}";
            //bindScript = bindScript + "else";
            //bindScript = bindScript + "{return false;}";
            //bindScript = bindScript + "";

            //this.btnVoid.Attributes.Add("onclick", bindScript);

        }

        #endregion

        #region SetLanguage

        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
        }
        /// <summary>
        /// for multi language
        /// </summary>
        private void SetLanguage()
        {
            //this.lblCodeSetupTitle.Text = this.GetLabel("SOM_IsolationReasonEntryTitle");
            this.lblTitle.Text = this.GetLabel("SOM_IsolationReason");
            this.lblIsolationReasonCode1.Text = this.GetLabel("SOM_IsolationReasonCode1", true);
            this.lblIsolationReasonCode2.Text = this.GetLabel("SOM_IsolationReasonCode2", true);
            this.lblIsolationReasonCode3.Text = this.GetLabel("SOM_IsolationReasonCode3", true);

            //this.btnSave.Text = this.GetLabel("COM_Save");
            //this.btnVoid.Text = this.GetLabel("SOM_VoidOperation");
            //this.btnNew.Text = this.GetLabel("SOM_NewIsolationReason");
            this.chkVoid.Text = this.GetLabel("SOM_Void", true);

            this.btnSave.Text = GetLabel("COM_Save");
            this.btnSaveAndNew.Text = GetLabel("SOM_SaveNew");
            this.btnSaveAndReturn.Text = GetLabel("COM_SaveAndReturn");
            this.btnCancel.Text = GetLabel("COM_Cancel");

            
        }
        #endregion

        #region self custom functinon
        /// <summary>
        /// when void the record,set the record readonly
        /// </summary>
        private void AfterVoid()
        {
            //this.txtIsolationReasonCode1.ReadOnly = true;
            //this.txtIsolationReasonCode2.ReadOnly = true;
            //this.txtIsolationReasonCode3.ReadOnly = true;
            this.txtIsolationReasonCode1.Enabled = false;
            this.txtIsolationReasonCode2.Enabled = false;
            this.txtIsolationReasonCode3.Enabled = false;
            //this.chkVoid.Enabled = false;
            
            //this.btnSave.Enabled = false;
            //this.btnVoid.Enabled = false;
            this.chkVoid.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnSaveAndNew.Enabled = false;
            this.btnSaveAndReturn.Enabled = false;
            
        }
        /// <summary>
        /// new\update\void the record
        /// </summary>
        /// <param name="isolateReasonID">in and out parm,be out parm just when the new a record</param>
        /// <param name="action">1 new ; 2 update ; 3 void </param>
        /// <returns>1 success; other fail</returns>
        private int Save(ref int isolateReasonID, int action)
        {
            return MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.UpdateIsolationReason(
                                                   ref isolateReasonID,
                                                   this.txtIsolationReasonCode1.Text.Trim(),
                                                   this.txtIsolationReasonCode2.Text.Trim(),
                                                   this.txtIsolationReasonCode3.Text.Trim(),
                                                   this.chkVoid.Checked,
                                                   this.ODMUser.ID,                                                  
                                                   this.ODMUser.ID,
                                                   action
                                                   );
        }

        private void DealSave(int type)
        {
            int id = 0;
            if (this.chkVoid.Checked == false)//update or new , not void
            {
                if (this.txtIsolationReasonCode1.Text.Trim() == String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code1NotNull"));
                    return;
                }
                else if (this.txtIsolationReasonCode2.Text.Trim() == String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code2NotNull"));
                    return;
                }
                else if (this.txtIsolationReasonCode3.Text.Trim() == String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code3NotNull"));
                    return;
                }
                if (this.hidID.Value == String.Empty)//new
                {
                    if (CheckUnique(true))
                    {
                        int mResult = Save(ref id, 1);
                        this.hidID.Value = id.ToString();
                        if (mResult == 1)
                        {
                            this.Alert(this.GetMessage("SaveSuccessfully"));

                            //Page.Response.Redirect("IsolateReasonEntry.aspx?isolationReasonID=" + id);
                            if (type == 1)//save
                            {
                                this.chkVoid.Enabled = true;
                                //this.Response.Redirect("IsolateReasonEntry.aspx?isolationReasonID=" + this.hidID.Value);
                                //string script = "<script language=javascript>function btnChange_Click(){window.location.href='IsolateReasonEntry.aspx?isolationReasonID='" + this.hidID.Value + "'} window.setTimeout(btnChange_Click,50);" + "</script>";
                                //this.Page.RegisterStartupScript("aaa", script);
                            }
                            else if (type == 2)//save and return
                            {
                                string script = "<script language=javascript>function btnChange_Click(){window.location.href='IsolateReasonList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                                this.Page.RegisterStartupScript("aaa", script);
                                //this.Response.Redirect("IsolateReasonList.aspx");
                            }
                            else if (type == 3)//save and new
                            {
                                string script = "<script language=javascript>function btnChange_Click(){window.location.href='IsolateReasonEntry.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                                this.Page.RegisterStartupScript("aaa", script);
                                //this.Response.Redirect("IsolateReasonEntry.aspx");
                            }
                        }
                       
                    }
                }
                else//update
                {
                    if (CheckUnique(false))
                    {
                        id = int.Parse(this.hidID.Value);
                        int mResult = Save(ref id, 2);
                        if (mResult == 1)
                        {
                            this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                            if (type == 1)//save
                            {
                                //this.chkVoid.Enabled = true;
                            }
                            else if (type == 2)//save and return
                            {
                                string script = "<script language=javascript>function btnChange_Click(){window.location.href='IsolateReasonList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                                this.Page.RegisterStartupScript("aaa", script);
                                //this.Response.Redirect("IsolateReasonList.aspx");
                            }
                            else if (type == 3)//save and new
                            {
                                string script = "<script language=javascript>function btnChange_Click(){window.location.href='IsolateReasonEntry.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                                this.Page.RegisterStartupScript("aaa", script);
                                //this.Response.Redirect("IsolateReasonEntry.aspx");
                            }
                        }
                        else
                        {
                            this.Alert(this.GetMessage("SaveUnsuccessfully"));
                        }
                    }
                }
            }
            else
            {
                if (this.hidID.Value != String.Empty)
                {
                    int mId = int.Parse(this.hidID.Value);
                    this.chkVoid.Checked = true;
                    int mResult = Save(ref mId, 3);

                    if (mResult == 1)
                    {
                        this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                        
                        if (type == 1)//save
                        {
                            AfterVoid();
                        }
                        else if (type == 2)//save and return
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='IsolateReasonList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("IsolateReasonList.aspx");
                        }
                        else if (type == 3)//save and new
                        {
                            string script = "<script language=javascript>function btnChange_Click(){window.location.href='IsolateReasonEntry.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                            this.Page.RegisterStartupScript("aaa", script);
                            //this.Response.Redirect("IsolateReasonEntry.aspx");
                        }
                    }
                    else
                    {
                        this.Alert(this.GetMessage("SOM_IsolateReasonVoidError"));
                    }


                }
            }
        }


        /// <summary>
        /// check if the code1,code2 or code3 is unique in the existed isolation reason records
        /// </summary>
        /// <param name="insert">true:insert(new) ; flase:update</param>
        /// <returns></returns>
        private bool CheckUnique(bool insert)
        {
            if (insert == true)
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.CheckIsolationReasonUnqiue(this.txtIsolationReasonCode1.Text.Trim(), 1, -1).Rows.Count > 0 && this.txtIsolationReasonCode1.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.CheckIsolationReasonUnqiue(this.txtIsolationReasonCode2.Text.Trim(), 1, -1).Rows.Count > 0 && this.txtIsolationReasonCode2.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.CheckIsolationReasonUnqiue(this.txtIsolationReasonCode3.Text.Trim(), 1, -1).Rows.Count > 0 && this.txtIsolationReasonCode3.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code3Unique"));
                    return false;
                }
                return true;
            }
            else
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.CheckIsolationReasonUnqiue(this.txtIsolationReasonCode1.Text.Trim(), 1, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtIsolationReasonCode1.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.CheckIsolationReasonUnqiue(this.txtIsolationReasonCode2.Text.Trim(), 1, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtIsolationReasonCode2.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDIsolateReason.CheckIsolationReasonUnqiue(this.txtIsolationReasonCode3.Text.Trim(), 1, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtIsolationReasonCode3.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Code3Unique"));
                    return false;
                }
                return true;
            }
        }
        #endregion

        #region Event
        /// <summary>
        /// save click, new and update 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {

            this.DealSave(1);
            
        }
       
        /// <summary>
        /// Void a isolateReason Record
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnVoid_Click(object sender, EventArgs e)
        {
            
        }
        #endregion

        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            this.DealSave(2);
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            this.DealSave(3);
        }

        protected void chkVoid_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkVoid.Checked)
            {
                string bindScript = "if (confirm('" + this.GetMessage("SOM_VoidSure") + "') )";

                bindScript = bindScript + "{return true;}";
                bindScript = bindScript + "else";
                bindScript = bindScript + "{return false;}";
                bindScript = bindScript + "";

                this.btnSave.Attributes.Add("onclick", bindScript);
                this.btnSaveAndNew.Attributes.Add("onclick", bindScript);
                this.btnSaveAndReturn.Attributes.Add("onclick", bindScript);
            }
            else
            {
                this.btnSave.Attributes.Clear();
                this.btnSaveAndReturn.Attributes.Clear();
                this.btnSaveAndNew.Attributes.Clear();
            }
        }

        
    }
}
